package com.weme.mine.activity

import account_server.v1.Account
import account_server.v1.Account.GetGoodsListReply
import android.content.Context
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.os.Build.VERSION_CODES.P
import android.os.Bundle
import android.text.TextUtils
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.jeremyliao.liveeventbus.LiveEventBus
import com.weme.common.arouter.ARouterPaths
import com.weme.common.base.BaseMvvmActivity
import com.weme.common.util.*
import com.weme.common.util.LiveConfig.NET_WORK_GOODS
import com.weme.common.view.SmoothImageView
import com.weme.mine.R
import com.weme.mine.databinding.ActivityCoinsBinding
import com.weme.mine.viewmode.CoinsViewMode
import dev.kratos.api.user_server.v1.GetAccountInfoReply
import dev.kratos.api.user_server.v1.Media

@Route(path = ARouterPaths.MINE_COINS_ACTIVITY)
class CoinsActivity:BaseMvvmActivity<ActivityCoinsBinding,CoinsViewMode>() {


    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).titleBar(mBinding.calView).init()
        mBinding.ibBack.setOnClickListener { finish() }
        mBinding.rcvList.apply {
            adapter =coinsAdapter
        }
        viewModel.getGoodsList()
        viewModel.getAccountInfo()
     LiveEventBus.get<Any>(NET_WORK_GOODS).observe(this){
            if(it is GetGoodsListReply){
                coinsAdapter.submitList(it.goodsList)
            }

        }

     LiveEventBus.get<Any>(LiveConfig.NET_WORK_MINE).observe(this) {
            if (it is GetAccountInfoReply) {
                if(TextUtils.isEmpty(it.account.coins.toString())){
                    mBinding.tvBalance.text = "0"
                }else{
                    mBinding.tvBalance.text = it.account.coins.toString()
                }
            }
        }
    }

    private val coinsAdapter = object : BaseQuickAdapter<Account.Goods, QuickViewHolder>() {
        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: Account.Goods?) {
            item?.apply {
                val tvOldMoney = holder.getView<TextView>(com.weme.common.R.id.tvOldMoney)
                tvOldMoney.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
                tvOldMoney.paint.isAntiAlias = true
                holder.setText(com.weme.common.R.id.tvName,this.goodName.toString())
                tvOldMoney.text = this.discount.toString()
                val imView = holder.getView<ImageView>(com.weme.common.R.id.imView)
                imView.loadImage(this.pic)
                holder.setText(com.weme.common.R.id.tvMoney,this.price.toString())

            }

        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            return QuickViewHolder(R.layout.item_coins, parent)
        }


    }
    override fun getViewModelClass(): Class<CoinsViewMode>  = CoinsViewMode::class.java
}